﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Module.Interfaces;
using Core.Module.Interfaces.Manager;
using Microsoft.Practices.Unity;
using Core.Module.Interfaces.Model;
using umbraco.interfaces;
using Core.Module.Model;

namespace Core.Module.Manager
{
    public class SiteManager:ISiteManager<IFrontend>
    {
        [Dependency]
        public INodeManager<IFrontend> NodeManager { get; set; }

        public List<ISite> Get()
        {
            return NodeManager.GetChildren(-1, "site")
                .Select(ni=>Get(ni))
                .ToList();
        }
        
        public ISite Get(int siteId)
        {
            IModuleNode sitenode=NodeManager.Get(siteId);
            if (sitenode == null) return null;
            return Get(sitenode);
        }

        public ISite Get(IModuleNode siteNode)
        {
            Site result = ObjectManager.Resolve<ISite>() as Site;
            result.Node = siteNode;            
            result.Homepage = NodeManager.GetPropertyAsNode(siteNode, "homePageId");
            int siteid=int.Parse(siteNode.Id);
            if (result.Homepage == null) result.Homepage = NodeManager.GetChild(siteid, "HomePage");

            result.Settings = NodeManager.GetPropertyAsNode(siteNode, "settingsId");
            if (result.Settings == null) result.Settings = NodeManager.GetChild(siteid, "Settings");
            return result;
        }
    }
}
